package org.brownflat.lighthouse.web;

import org.displaytag.decorator.DisplaytagColumnDecorator;
import org.displaytag.properties.MediaTypeEnum;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
public class DateTimeDecorator implements DisplaytagColumnDecorator {
    @Override
    public Object decorate(Object columnValue, javax.servlet.jsp.PageContext pageContext, MediaTypeEnum media) {
        DateTime value = (DateTime) columnValue;
        DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
        String timezone = (String) pageContext.getAttribute("timezone");
        if(timezone == null || timezone.isEmpty()) {
            timezone = "UTC";
        }
        return format.withZone(DateTimeZone.forID(timezone)).print(value);
    }
}
